#pragma once
#include <malloc.h>
#include <stddef.h>
#include <stdint.h>
// *****************************固定大小内存池***********************************
typedef struct memory_pool_node_s {
  void *memory;
  void *next;
} memory_pool_node_t;

typedef struct fixed_size_memory_pool_s {
  memory_pool_node_t *node;
  size_t memory_size;
} fixed_size_memory_pool_t;

int init_fixed_size_memory_pool(fixed_size_memory_pool_t *pool,
                                size_t memory_size);
void close_fixed_size_memory_pool(fixed_size_memory_pool_t *pool);
int fixed_size_memory_pool_pre_alloc(fixed_size_memory_pool_t *pool,
                                     size_t size);
void *fixed_size_memory_pool_alloc(fixed_size_memory_pool_t *pool);
void fixed_size_memory_pool_free(void *ptr);

/************************************可变大小内存池********************************************/
typedef struct complex_memory_pool_s {
  size_t block_size;
  size_t line_init_size;
  size_t line_size;
  fixed_size_memory_pool_t *lines;
} complex_memory_pool_t;

int init_complex_memory_pool(complex_memory_pool_t *pool, size_t block_size,
                             size_t init_line, size_t line_init_size);
void close_complex_memory_pool(complex_memory_pool_t *pool);
void *complex_memory_pool_alloc(complex_memory_pool_t *pool, size_t size);
void complex_memory_pool_free(void *ptr);

// ******************************通用内存池*****************************************

/*
level:
  0 : 块大小为4;
  1 ：块大小为4 * 256;
  2 ：块大小为4 * 256 * 256;
  3 ：块大小为4 * 256 * 256 * 256;
  ...
  7 : 块大小为4 * (256 ^ 7);
count: 初始化大小;
*/ 
int memory_pool_level_init(uint8_t level, size_t count);
void *memory_pool_alloc(size_t size);
void memory_pool_free(void *ptr);
void *memory_pool_realloc(void *ptr, size_t osize, size_t size);